package com.hghivln.controller.doctor;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.hghivln.constants.Constants;
import com.hghivln.pojo.domain.CareOrder;
import com.hghivln.pojo.domain.CareOrderItem;
import com.hghivln.pojo.vo.AjaxResult;
import com.hghivln.pojo.vo.CareHistoryVo;
import com.hghivln.service.CareService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.math.BigDecimal;
import java.util.*;

/**
 * 处方发药
 */
@RestController
@RequestMapping("doctor/handleMedicine")
public class HandleMedicineController {

    @Reference
    private CareService careService;

    /**
     * 根据挂号ID查询已支付的药品处方信息及详情
     */
    @GetMapping("getChargedCareHistoryOnlyMedicinesByRegId/{regId}")
    public AjaxResult getChargedCareHistoryOnlyMedicinesByRegId(@PathVariable @Validated @NotBlank(message = "挂号单号不能为空") String regId) {
        CareHistoryVo careHistoryVo = careService.selectCareHistoryByRegId(regId);
        if (ObjectUtils.isEmpty(careHistoryVo)) {
            return AjaxResult.fail("【" + regId + "】的挂号单没有对应的病例信息，请核对后再查询");
        }
        List<CareOrder> careOrderList = careService.selectCareOrdersByChId(careHistoryVo.getChId());
        if (CollectionUtil.isEmpty(careOrderList)) {
            return AjaxResult.fail("【" + regId + "】的挂号单没相关的处方信息，请核对后再查询");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("careHistory", careHistoryVo);
        map.put("careOrders", Collections.EMPTY_LIST);
        //声明一个可以存放careOrders的集合
        List<Map<String, Object>> mapList = new ArrayList<>();

        for (CareOrder careOrder : careOrderList) {
            if (careOrder.getCoType().equals(Constants.CO_TYPE_MEDICINES)) {
                Map<String, Object> careOrderMap = BeanUtil.beanToMap(careOrder);
                careOrderMap.put("careOrderItems", Collections.EMPTY_LIST);
                List<CareOrderItem> careOrderItems = careService.selectCareOrderItemByCoId(careOrder.getCoId(), Constants.ORDER_DETAILS_STATUS_1);
                if (CollectionUtils.isEmpty(careOrderItems)) {
                    continue;
                } else {
                    BigDecimal allAmount = new BigDecimal("0");
                    for (CareOrderItem careOrderItem : careOrderItems) {
                        allAmount = allAmount.add(careOrderItem.getAmount());
                    }
                    careOrderMap.put("careOrderItems", careOrderItems);
                    careOrderMap.put("allAmount", allAmount);
                    mapList.add(careOrderMap);
                }
                careOrderMap.put("careOrderItems", careOrderItems);
            }
        }
        if (CollectionUtils.isEmpty(mapList)) {
            return AjaxResult.fail("【" + regId + "】的挂号单没已支付的药品处方信息，请核对后再查询");
        } else {
            map.put("careOrders", mapList);
            return AjaxResult.success(map);
        }
    }

    /**
     * 处方发药
     */
    @PostMapping("doMedicine")
    public AjaxResult doMedicine(@RequestBody @Validated @NotEmpty(message = "开诊明细ID不能为空") List<String> itemIds) {
        String msg = careService.doMedicine(itemIds);
        return AjaxResult.success(msg);
    }
}
